package com.tc.travel.submitter.executor;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tc.travel.submitter.service.SessionBo;
import com.tc.travel.util.Consts;
import com.tc.travel.util.HtmlUtil;
import com.tc.travel.util.HttpClientUtil;
import com.tc.travel.util.PropertyUtil;
import com.tc.travel.util.SleepUtil;

@Component
public class SessionMaintainerExecutor {
	private static Logger log = Logger.getLogger(SessionMaintainerExecutor.class);

	@Autowired
	private SessionBo sessionBo;

	public void execute() {
		try {
			log.info("refreshing cookie...");

			// 睡眠5到10分鐘
			SleepUtil.sleepRandom(0, 300);
			String cookie = sessionBo.getCookie();
			openAddEntityPage(cookie);
		} catch (Throwable e) {
			log.error("SessionMaintainerExecutor error: " + e.getMessage(), e);
		}
	}

	public String openAddEntityPage(String cookie) {
		String url = PropertyUtil.destUrlVisaEdit;

		Map<String, String> params = new HashMap<String, String>();
		// params.put("mode", "add");

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Cookie", cookie);
		headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		headers.put("Accept-Encoding", "gzip, deflate, sdch, br");
		headers.put("Accept-Language", "en-US,en;q=0.8,ja;q=0.6");
		headers.put("Connection", "keep-alive");
		headers.put("Host", "churenkyosystem.com");
		headers.put("Referer", "https://churenkyosystem.com/member/top.php");
		headers.put("Upgrade-Insecure-Requests", "1");
		headers.put("User-Agent", Consts.USER_AGENT);

		String html = HttpClientUtil.doGet(url, params, headers);

		return HtmlUtil.getPageKey(html);
	}

}
